﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BackEnd.Models;
using BackEnd.Common;

namespace BackEnd.Controllers
{
    public class CurrencyController : Controller
    {
        //
        // GET: /Currency/

        [CustomAuthorize(Action = Constants.AdminActions.ListCurrency)]
        public ActionResult Index()
        {
            return View();
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateCurrency)]
        public ActionResult Create()
        {
            CurrencyModel currencyModel = new CurrencyModel();
            List<Countrys> listCountry = new CountryBL().GetAll();
            currencyModel.ListCountry = listCountry;
            return View(currencyModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateCurrency)]
        public ActionResult Create(CurrencyModel currencyModel)
        {
            if (string.IsNullOrWhiteSpace(currencyModel.Currency))
            {
                ModelState.AddModelError("Currency", "Currency is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {                
                new CurrencyBL().Insert(new Currencys 
                        { 
                            Currency = currencyModel.Currency, 
                            Country = new CountryBL().GetById(currencyModel.CountryId).Country });

                return RedirectToAction("Index", "Currency");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditCurrency)]
        public ActionResult Edit(int id)
        {
            CurrencyModel currencyModel = new CurrencyModel();
            Currencys currency = new CurrencyBL().GetById(id);
            currencyModel.Currency = currency.Currency;
            Countrys country = new CountryBL().GetByName(currency.Country);
            currencyModel.CountryId = country!=null ? country.Id : -1;
            currencyModel.ListCountry = new CountryBL().GetAll();
            return View(currencyModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditCurrency)]
        public ActionResult Edit(CurrencyModel currencyModel)
        {
            if (string.IsNullOrWhiteSpace(currencyModel.Currency))
            {
                ModelState.AddModelError("Currency", "Currency is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new CurrencyBL().Update(new Currencys 
                    { 
                        Id = currencyModel.Id,
                        Currency = currencyModel.Currency,
                        Country = new CountryBL().GetById(currencyModel.CountryId).Country });
                return RedirectToAction("Index", "Currency");
            }
        }

        public JsonResult Search(string name, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            List<Currencys> listCurrencys = new CurrencyBL().Search(name, pageIndex, pageSize, out totalRecord);

            ListObjectModel<Currencys> listObject = new ListObjectModel<Currencys> { ListItem = listCurrencys, PageIndex = pageIndex, PageSize = pageSize, TotalRecord = totalRecord };
            return Json(listObject, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteCurrency)]
        public JsonResult Delete(string listId)
        {            

            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new CurrencyBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
